
import { Timer, TimerStatus } from "./Timer"

let timerIndex:number = 0
export class TimerManager{
    static _instance: TimerManager = null
    static GetInstance():TimerManager{
        if (this._instance == null) {
            this._instance = new TimerManager()
        }
        return this._instance
    }
    CreateTimer(interval: number = -1, loop: boolean = false, callback: Function = null): Timer {
        let timer = new Timer(this.GetTimerIndex(), interval, loop, callback)
        this.timers.set(timer.index, timer)
        return timer
    }
    timers: Map<number, Timer> = new Map()
    constructor() {
       
    }
    Update(dt: number) {
        this.timers.forEach((timer,index)=>{
            timer.Update(dt)
        })
    }

    Destroy() {
        this.timers.clear()
    }

    GetTimerIndex(): number {
        return ++ timerIndex 
    }

    GetTimer(timerIndex:number): Timer {       
        return this.timers.get(timerIndex)
    }
    DestroyTimer(timerIndex:number){
        this.timers.delete(timerIndex)
    }
}